package com.example.book.service;

import com.example.book.mapper.BookMapper;
import com.example.book.mapper.UserMapper;
import com.example.book.model.BookInfo;
import com.example.book.model.PageRequest;
import com.example.book.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class BookService {
@Autowired
    BookMapper bookMapper;
    public void addBook(BookInfo bookInfo) {
        bookMapper.addBook(bookInfo);
    }

    public PageResult<BookInfo> getList(PageRequest pageRequest) {
        Integer count=bookMapper.count();
    ArrayList<BookInfo> arrayList=bookMapper.getList(pageRequest);
    PageResult<BookInfo> bookInfoPageResult=new PageResult<>();
    for(BookInfo bookInfo:arrayList){
        if(bookInfo.getStatus()==1){
            bookInfo.setStatusCN("可借阅");
        }else if(bookInfo.getStatus()==2){
            bookInfo.setStatusCN("不可借阅");
        }
    }
    bookInfoPageResult.setRecords(arrayList);
    bookInfoPageResult.setCount(count);
    bookInfoPageResult.setPageRequest(new PageRequest(pageRequest.getPageNum(),pageRequest.getPageSize(),pageRequest.getOffset()));
    return bookInfoPageResult;
    }

    public Integer update(BookInfo bookInfo) {
        return bookMapper.update(bookInfo);
    }
}
